#导入python的 numpy matplotlib  pandas库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#绘图
import seaborn as sns


#导入数据
path='生物医学语音数据.csv'
dataframe= pd.read_csv(path,sep=',')
dataframe =dataframe.drop(['name'],axis=1)
#查看头部数据
print(dataframe.head(10))

# 查看数据集一些信息
#查看数据的类型，有没有空值 ，缺失数据
print("数据集的基本信息: ")
print(dataframe.info())
print("数据集的标签:")
print(dataframe.columns)


feature_cols = list(dataframe.columns[1:16]) + list(dataframe.columns[18:])
target_col = dataframe.columns[17]
print("特征列")
print(feature_cols)
print("目标判定列")
print(target_col)

# 患病和健康的样本
# sns.countplot(dataframe['status'].values)
# plt.xlabel('Class Values')
# plt.ylabel('Class Counts')
# plt.show()

#将特征列全部选取出来，然后制作相关系数矩阵
feature_data= dataframe.iloc[:,1:]
#查看一下数据拼接情况 返回每列是否有空值
feature_data.head(10)
temp=feature_data.isnull().any();
print(temp)

#计算协方差
corr = feature_data.corr()
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.figure(figsize=(11, 7))
sns.heatmap(feature_data.corr(method='spearman').round(5),cmap="coolwarm",vmin=0, vmax=1)
plt.show()

# plt.subplots(figsize=(20, 50))
# heatmap=sns.heatmap(feature_data.corr(method='spearman').round(5), annot=True,cmap='coolwarm', linewidths=0.5,vmin=0, vmax=1)
# 将字体竖着显示
#然后我们现在来看看具体的情况
# check_data= pd.concat([dataframe.iloc[:,3:15],dataframe.iloc[:,[17]]],axis=1)

check_data1 = dataframe[['PPE','spread1','spread2','MDVP:Shimmer','MDVP:APQ','Shimmer:APQ5','MDVP:Shimmer(dB)',
                       'Shimmer:APQ3','Shimmer:DDA','D2','status']]
check_data2 = dataframe[['MDVP:Jitter(Abs)','RPDE','MDVP:PPQ','MDVP:Jitter(%)','MDVP:RAP','Jitter:DDP','DFA','NHR'
                        ,'status']]
check_data3 = dataframe[['MDVP:Fhi(Hz)','HNR','MDVP:Flo(Hz)','MDVP:Fo(Hz)','status']]

sns.pairplot(check_data1, hue="status")

plt.show()
sns.pairplot(check_data2, hue="status")
plt.show()
sns.pairplot(check_data3, hue="status")
plt.show()

new_data=dataframe
# #将数据分割成数据 X 标签Y
y =new_data.iloc[16].values
X = new_data.drop('status',axis=1).values
print(new_data)

from sklearn.preprocessing import StandardScaler
#去均值和方差归一化。且是针对每一个特征维度来做的，而不是针对样本
X_std = StandardScaler().fit_transform(X)
print (X_std)

print('协方差矩阵: \n%s' %np.cov(X_std.T))

#样本协方差
cov_mat = np.cov(X_std.T)
# 计算特征值和特征向量
eig_vals, eig_vecs = np.linalg.eig(cov_mat)

print('特征向量 \n%s' %eig_vecs)
print('\特征值 \n%s' %eig_vals)

#列出特征值 特征向量的元组
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
print ('----------')
print('特征向量')
for i in eig_pairs:
    print(i[0])

# 将数据进行处理 分离 出 x 和 y
feature_cols = list(new_data.columns[0:16]) + list(new_data.columns[17:])
target_col = new_data.columns[16]
print("Feature columns:\n{}".format(feature_cols))
print("\nTarget column: {}".format(target_col))
X_all = new_data[feature_cols]
y_all = new_data[target_col]


# 训练数据和测试数据
num_all = new_data.shape[0]
num_train = 150 # about 75% of the data
num_test = num_all - num_train
# 交叉验证 ，分离出测试数据 ，测试标签， 训练数据 ，训练标签
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test,random_state=5)
print("训练集: {} samples".format(X_train.shape[0]))
print("测试集: {} samples".format(X_test.shape[0]))

from sklearn.metrics import f1_score
from sklearn.naive_bayes import GaussianNB
from time import time
#训练数据函数
def train_classifier(clf, X_train, y_train):
    start = time()
    clf.fit(X_train, y_train)
    end = time()
    print("训练数据使用的时间 {:.4f}".format(end - start))


# f1分数
def predict_labels(clf, features, target):
    start = time()
    y_pred = clf.predict(features)
    end = time()

    print("在 {:.4f}秒 内预测.".format(end - start))
    return f1_score(target.values, y_pred, pos_label=1)


def train_predict(clf, X_train, y_train, X_test, y_test):
    print("使用{} 算法 训练集的大小 {}. . .".format(clf.__class__.__name__, len(X_train)))

    train_classifier(clf, X_train, y_train)

    print("训练集的F1 得分: {:.4f}.".format(predict_labels(clf, X_train, y_train)))
    print("测试集的F1 得分: {:.4f}.".format(predict_labels(clf, X_test, y_test)))
# 准备训练数据
X_train_50 = X_train[:50]
y_train_50 = y_train[:50]

X_train_100 = X_train[:100]
y_train_100 = y_train[:100]

X_train_150 = X_train[:150]
y_train_150 = y_train[:150]
#训练的算法模型 GaussianNB
clf = GaussianNB()
train_predict(clf,X_train_150,y_train_150,X_test,y_test)

# 通过模型得出的结果绘制ROC AUE 曲线
from sklearn.metrics import roc_curve, auc
y_pred=clf.predict(X_test)
y_proba=clf.predict_proba(X_test)
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test,y_proba[:,1])
roc_auc = auc(false_positive_rate, true_positive_rate)
plt.figure(figsize=(5,5))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
#真阳性假阳性热图
from sklearn.metrics import classification_report,confusion_matrix
cm=confusion_matrix(y_test,y_pred)
print(cm)
sns.heatmap(cm,annot=True)
plt.show()
